package com.carsa.blackberry.domain.dto;

import java.util.Enumeration;
import java.util.Vector;

import com.carsa.blackberry.domain.services.webServices.impl.ZSdareMotivos.ZSdareMotivo;
import com.carsa.blackberry.util.XMLHandler;
import com.carsa.blackberry.util.XMLPortable;

/**
 * @author Nahuel Barrios.
 * @since 23/08/2010.
 * @version 1.0
 */
public class ReasonDTO implements XMLPortable {

    private static final String XML_ELEMENT_REASON_DESCRIPT = "reasonDescript";
    private static final String XML_ELEMENT_REASON_CODE     = "reasonCode";
    private static final String XML_ELEMENT_SUBREASONS      = "subreasons";

    private String              _reasonCode;
    private String              _reasonDescript;

    private Vector              _subreasons                 = new Vector();

    public Vector getSubreasons() {
	return this._subreasons;
    }

    public ReasonDTO() {
	super();
    }

    public ReasonDTO(String xmlSource) {
	this.initializeFromXML(xmlSource);
    }

    public ReasonDTO(ZSdareMotivo unMotivo) {
	this._reasonCode = unMotivo.getGrund();
	this._reasonDescript = unMotivo.getDdtxt();
    }
    
    public SecondReasonDTO getSubreason(String submo){
	Enumeration subreasons= this._subreasons.elements();
	while (subreasons.hasMoreElements()) {
	    SecondReasonDTO aSecondReason = (SecondReasonDTO) subreasons.nextElement();
	    
	    if(aSecondReason.getCode().equals(submo))
		return aSecondReason;
        }
	return null;
    }

    public void addSecondReason(SecondReasonDTO secondReason) {
	if (secondReason != null)
	    this._subreasons.addElement(secondReason);
    }

    public void initializeFromXML(String xmlSource) {
	XMLHandler xml = new XMLHandler(xmlSource);

	_reasonCode = xml.getElementValue(XML_ELEMENT_REASON_CODE);
	_reasonDescript = xml.getElementValue(XML_ELEMENT_REASON_DESCRIPT);

	xml = new XMLHandler(xml.getElementValue(XML_ELEMENT_SUBREASONS));
	int elementsCount = xml.getElementCount();

	int idx = 0;
	for (idx = 0; idx < elementsCount; idx++) {
	    _subreasons.addElement(new SecondReasonDTO(xml.getFirstElementValueOverIteration()));
	}

	// _secondReasonCode= xml.getElementValue(XML_ELEMENT_SECOND_REASON_CODE);
	// _secondReasonDescript = xml.getElementValue(XML_ELEMENT_SECOND_REASON_DESCRIPT);
    }

    public String toXMLString() {
	XMLHandler xml = new XMLHandler("aReason", null, null);

	xml.addElementAtRoot(XML_ELEMENT_REASON_CODE, null, _reasonCode);
	xml.addElementAtRoot(XML_ELEMENT_REASON_DESCRIPT, null, _reasonDescript);

	XMLHandler xmlSubreasons = new XMLHandler(XML_ELEMENT_SUBREASONS, null, null);
	Enumeration subreasons = _subreasons.elements();
	while (subreasons.hasMoreElements()) {
	    SecondReasonDTO secondReason = (SecondReasonDTO) subreasons.nextElement();
	    xmlSubreasons.addElementAtRoot(secondReason.toXMLString());
	}
	xml.addElementAtRoot(xmlSubreasons.toXMLString());

	return xml.toXMLString();
    }

    public String toString() {
	return "ReasonDTO [  _reasonCode=" + _reasonCode + ",  _reasonDescript=" + _reasonDescript + "  ]";
    }

    /**
     * @return the reasonCode
     */
    public String getReasonCode() {
	return _reasonCode;
    }

    /**
     * @param reasonCode
     *            the reasonCode to set
     */
    public void setReasonCode(String reasonCode) {
	this._reasonCode = reasonCode;
    }

    /**
     * @return the reasonDescript
     */
    public String getReasonDescript() {
	return _reasonDescript;
    }

    /**
     * @param reasonDescript
     *            the reasonDescript to set
     */
    public void setReasonDescript(String reasonDescript) {
	this._reasonDescript = reasonDescript;
    }

    public Vector getSubreasonsWithFlepa(String reasonFlepaX) {
	Vector ret = new Vector();

	Enumeration subreasons = this._subreasons.elements();
	while (subreasons.hasMoreElements()) {
	    SecondReasonDTO aSecondReason = (SecondReasonDTO) subreasons.nextElement();
	    if (aSecondReason.isFlepaSetted())
		ret.addElement(aSecondReason);
	}

	return ret;
    }

}
